package com.dhecp.project.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.OperateLevelInfo;
import com.dhecp.project.base.mapper.OperateLevelInfoMapper;
import com.dhecp.project.base.service.OperateLevelInfoService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 运营职级管理 服务层
 * @author DH-Xxj
 * @version 2022年6月21日
 */
@Service
public class OperateLevelInfoServiceImpl implements OperateLevelInfoService {

	@Autowired
	private OperateLevelInfoMapper operateLevelInfoMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 通过运营职级编码【guid】查询
	 * @param guid 必要参数：运营职级行唯一值
	 * @return OperateLevelInfo - 实体
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public OperateLevelInfo queryOperateLevelInfoByGuid(Map<String, Object> params) throws Exception {
		if (params.get("guid") == null || params.get("guid") == ""){
    		throw new RuntimeException("请提供必要的参数[运营职级行唯一值]：guid"); 
    	}
		return operateLevelInfoMapper.findOperateLevelInfoByGuid(params.get("guid").toString());
	}

	/**
	 * 通过运营职级编码【vId】查询
	 * @param vId 必要参数：运营职级编码
	 * @return OperateLevelInfo - 实体
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public OperateLevelInfo queryOperateLevelInfoByVId(Map<String, Object> params) throws Exception {
		if (params.get("vId") == null || params.get("vId") == ""){
    		throw new RuntimeException("请提供必要的参数[运营职级编码]：vId"); 
    	}
		return operateLevelInfoMapper.findOperateLevelInfoByVId(params.get("vId").toString());
	}
	
	/**
	 * 按条件查询
	 * @param params
	 * @param nowherestr 若存在（任意值），表示不进行条件过滤
	 * @param nodatascode 若存在（任意值），表示不进行数据权限控制
	 * @return List-OperateLevelInfo
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<OperateLevelInfo> queryOperateLevelInfoList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));		//数据权限查询条件SQL语句
        PageUtils.startPage();
        return operateLevelInfoMapper.findOperateLevelInfoList(params);
    }
	
	/**
	 * 按条件查询生效数据
	 * @param params
	 * @param nowherestr 若存在（任意值），表示不进行条件过滤
	 * @param nodatascode 若存在（任意值），表示不进行数据权限控制
	 * @return List-OperateLevelInfo
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
    public List<OperateLevelInfo> queryEnableList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));		//数据权限查询条件SQL语句
        PageUtils.startPage();
        return operateLevelInfoMapper.findEnableList(params);
    }

	/**
	 * 新增
	 * @param infoItems 对应实体类的json格式字符串
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult insertOperateLevelInfo(Map<String, Object> params) {
        List<OperateLevelInfo> operateLevelInfoList = JSON.parseArray(params.get("infoItems").toString(), OperateLevelInfo.class);
        if (operateLevelInfoList == null || operateLevelInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (OperateLevelInfo operateLevelInfo : operateLevelInfoList) {
        	if (operateLevelInfo.getGuid() == null || "".equals(operateLevelInfo.getGuid())){
        		operateLevelInfo.setGuid(RandomValueUtils.getGUID());
        	}
        	operateLevelInfo.setCreateUser(ShiroUtils.getUserId());
        	operateLevelInfo.setCreateTime(new Date());
        	operateLevelInfo.setUpdateUser(ShiroUtils.getUserId());
        	operateLevelInfo.setUpdateTime(new Date());
        	operateLevelInfoMapper.insert(operateLevelInfo);
        }
        return AjaxResult.success("操作成功");
    }

	/**
	 * 删除
	 * @param infoItems 对应实体类的json格式字符串（必要字段[guid]）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult deleteOperateLevelInfo(Map<String, Object> params){
        List<OperateLevelInfo> operateLevelInfoList = JSON.parseArray(params.get("infoItems").toString(), OperateLevelInfo.class);
        if (operateLevelInfoList == null || operateLevelInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (OperateLevelInfo operateLevelInfo : operateLevelInfoList) {
        	if (operateLevelInfo.getGuid() == null || "".equals(operateLevelInfo.getGuid())){
        		operateLevelInfo.setGuid("NOGUID");
        	}
        	operateLevelInfoMapper.deleteByGuid(operateLevelInfo.getGuid());
        }
        return AjaxResult.success("操作成功");
    }

	/**
	 * 修改禁用标志（逻辑删除）
	 * @param infoItems 对应实体类的json格式字符串（必要字段[guid]，[bDeleted]）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult updateOperateLevelInfoBDeleted(Map<String, Object> params){
        List<OperateLevelInfo> operateLevelInfoList = JSON.parseArray(params.get("infoItems").toString(), OperateLevelInfo.class);
        if (operateLevelInfoList == null || operateLevelInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (OperateLevelInfo operateLevelInfo : operateLevelInfoList) {
        	if (operateLevelInfo.getGuid() == null || "".equals(operateLevelInfo.getGuid())){
        		operateLevelInfo.setGuid("NOGUID");
        	}
        	if (operateLevelInfo.getbDeleted() == null){
        		operateLevelInfo.setbDeleted(false);
        	}
        	operateLevelInfo.setUpdateUser(ShiroUtils.getUserId());
        	operateLevelInfo.setUpdateTime(new Date());
        	operateLevelInfoMapper.updateBDeleted(operateLevelInfo);
        }
        return AjaxResult.success("操作成功");
    }

	/**
	 * 修改信息
	 * @param infoItems 对应实体类的json格式字符串（必要字段[guid]）
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult updateOperateLevelInfo(Map<String, Object> params){
        List<OperateLevelInfo> operateLevelInfoList = JSON.parseArray(params.get("infoItems").toString(), OperateLevelInfo.class);
        if (operateLevelInfoList == null || operateLevelInfoList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (OperateLevelInfo operateLevelInfo : operateLevelInfoList) {
        	if (operateLevelInfo.getGuid() == null || "".equals(operateLevelInfo.getGuid())){
        		operateLevelInfo.setGuid("NOGUID");
        	}
        	operateLevelInfo.setUpdateUser(ShiroUtils.getUserId());
        	operateLevelInfo.setUpdateTime(new Date());
        	operateLevelInfoMapper.update(operateLevelInfo);
        }
        return AjaxResult.success("操作成功");
    }
    
}
